package com.ruoyi.project.wx.api;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.project.wx.domain.GoodsProductDO;
import com.ruoyi.project.wx.domain.MemberCartDO;
import com.ruoyi.project.wx.domain.MemberUserDO;
import com.ruoyi.project.wx.service.GoodsGoodsService;
import com.ruoyi.project.wx.service.GoodsProductService;
import com.ruoyi.project.wx.service.MemberCartService;
import com.ruoyi.project.wx.service.MemberUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by HP on 2019/5/29.
 */
@Controller
@RequestMapping("/api/memberCart")
public class ApiMemberCartController {

    @Autowired
    private MemberCartService memberCartService;
    @Autowired
    private MemberUserService memberUserService;
    @Autowired
    private GoodsProductService goodsProductService;
    @Autowired
    private GoodsGoodsService goodsGoodsService;

    /***
     * 获取购物车数量
     * @param map
     * @return
     */
    @RequestMapping("/getCountCart")
    @ResponseBody
    public R getCountCart(@RequestBody Map<String, Object> map){
        try {
            if (!map.containsKey("userId") || StringUtils.isBlank(map.get("userId").toString())

            ){
                return R.error("参数错误");
            }

            return memberCartService.getCountCart(map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("添加购物车失败");
        }
    }

    /**
     * 购物车添加
     * @param map
     * @return
     */
    @RequestMapping("/addCart")
    @ResponseBody
    public R addCart(@RequestBody Map<String, Object> map){
        try {
            if (!map.containsKey("userId") || StringUtils.isBlank(map.get("userId").toString())
                    || !map.containsKey("goodsId") || StringUtils.isBlank(map.get("goodsId").toString())
                    || !map.containsKey("productId") || StringUtils.isBlank(map.get("productId").toString())
                    || !map.containsKey("num") || StringUtils.isBlank(map.get("num").toString())
                    ){
                return R.error("购物车添加参数错误");
            }
            String num = map.get("num").toString();
            if ("0".equals(num)) {
                return R.error("商品数量不能为0");
            }
            return memberCartService.add(map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("添加购物车失败");
        }
    }

    /**
     * 购物车查询
     * @param map
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public R getList(@RequestBody Map<String, Object> map){
        try {
            if (!map.containsKey("userId") || StringUtils.isBlank(map.get("userId").toString())){
                return R.error("购物车参数错误");
            }
            String userId = map.get("userId").toString();
            MemberUserDO userDO = memberUserService.get(userId);
            if (userDO==null){
                return R.error("登录信息异常");
            }
            //查询所有地址
            List<Map<String, Object>> list = memberCartService.getCartListBuyUser(map);
            return R.ok().put("data", list);
        } catch (Exception e) {
            return R.error("购物车查询失败");
        }
    }

    /**
     * 购物车删除
     * @param map
     * @return
     */
    @RequestMapping("/cartRemove")
    @ResponseBody
    public R cartRemove(@RequestBody Map<String, Object> map){
        try {
            if (!map.containsKey("cartList") || StringUtils.isBlank(map.get("cartList").toString())
                    ||!map.containsKey("userId") || StringUtils.isBlank(map.get("userId").toString())
                    ){
                return R.error("参数错误");
            }
            List<String> cartList = (List<String>)(map.get("cartList")); //商品集合
            map.put("ids", cartList);
            memberCartService.removeByUserIdAndCardId(map);
            return R.ok("删除成功");
        } catch (Exception e) {
            return R.error("购物车删除失败");
        }
    }

    /**
     * 购物车修改
     * @param map
     * @return
     */
    @RequestMapping("/editCart")
    @ResponseBody
    public R addAndSub(@RequestBody Map<String, Object> map){
        try {
            if (!map.containsKey("cartId") || StringUtils.isBlank(map.get("cartId").toString())
                    || !map.containsKey("num") || StringUtils.isBlank(map.get("num").toString())
                    || !map.containsKey("userId") || StringUtils.isBlank(map.get("userId").toString())
                    ){
                return R.error("参数错误");
            }
            return memberCartService.editCart(map);
        } catch (Exception e) {
            return R.error("购物车修改失败");
        }
    }

    /**
     * 购物车 确认订单
     * @param map
     * @return
     */
    @RequestMapping("/enter")
    @ResponseBody
    public R enter(@RequestBody Map<String, Object> map){
        try {
            if (!map.containsKey("cartList") || StringUtils.isBlank(map.get("cartList").toString())
                    || !map.containsKey("userId") || StringUtils.isBlank(map.get("userId").toString())){
                return R.error("参数错误");
            }
            List<String> cartIds = (List<String>)(map.get("cartList"));
            String userId = map.get("userId").toString();
            MemberUserDO userDO = memberUserService.get(userId);
            if (userDO==null){
                return R.error("登录信息异常");
            }
            BigDecimal allPostFee = new BigDecimal(0);
            int allNum = 0;
            BigDecimal allPrice = new BigDecimal(0);
            //判断购买量
            for (String cartId : cartIds) {
                MemberCartDO memberCartDO = memberCartService.get(cartId);
                String productId = memberCartDO.getProductId();
                int num = memberCartDO.getNum();  //购买量
                allNum = allNum+num;
                GoodsProductDO productDO = goodsProductService.get(productId);
                BigDecimal price = productDO.getPrice();

                allPrice = allPrice.add(price.multiply(new BigDecimal(num)));
                int min = productDO.getBuyMin();
                int max = productDO.getBuyMax();
                if (num < min) {
                    String goodsId = memberCartDO.getGoodsId();
                    String goodsName = goodsGoodsService.get(goodsId).getName();
                    return R.error(goodsName+" 最小购买量为"+min);
                } else if (num > max) {
                    String goodsId = memberCartDO.getGoodsId();
                    String goodsName = goodsGoodsService.get(goodsId).getName();
                    return R.error(goodsName+" 最大购买量为"+max);
                }
                //计算运费
                BigDecimal postFee = productDO.getPostFee();
                allPostFee = allPostFee.add(postFee.multiply(new BigDecimal(num)));
            }

            List<Map<String,Object>> list = new ArrayList<>();
            list = memberCartService.getCartListBuyIds(cartIds);
            return R.ok().put("data", list).put("allPostFee",allPostFee).put("allNum",allNum).put("allPrice",allPrice);
        } catch (Exception e) {
            return R.error("购物车确认失败");
        }
    }


}
